package member;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

import util.*;

public class UpdateAction extends DispatchAction {
	
	public ActionForward update(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		MemberForm mf = (MemberForm)form;
		
		MemberDAO memberDAO = new MemberDAO();
		
		User user = (User)request.getSession().getAttribute("user");
		
		//MemberForm to MemberDTO
		Member member = new Member();
				
		member.setMid(user.getMid());
		member.setMpass(mf.getMpass());
		member.setMname(user.getUserInfo().getMname());
		member.setLvno(user.getUserInfo().getLvno());
		member.setMemail(mf.getMemail());
		member.setMbirth(user.getUserInfo().getMbirth());
		member.setMaddressh(mf.getMaddressh());
		member.setMaddresso(mf.getMaddresso());
		member.setMphoneh(mf.getMphoneh());
		member.setMphoneo(mf.getMphoneo());
		member.setMregdate(user.getUserInfo().getMregdate());
		member.setMintro(mf.getMintro());
		
		member.setMoemail(mf.getMoemail());
		member.setMobirth(mf.getMobirth());
		member.setMoaddressh(mf.getMoaddressh());
		member.setMoaddresso(mf.getMoaddresso());
		member.setMophoneh(mf.getMophoneh());
		member.setMophoneo(mf.getMophoneo());
		member.setMointro(mf.getMointro());
		member.setMophoto(mf.getMophoto());
		member.setMomailing(mf.getMomailing());
		
		memberDAO.updateMpass(member);
		memberDAO.updateInfos(member);
		memberDAO.updateIsOpened(member);
			
		if(mf.getMphoto().getFileSize()>0){		
						
			ServletContext sc = request.getSession().getServletContext();		
			String originalFileName = mf.getMphoto().getFileName();
			
			String deleteFileName = sc.getRealPath("/uploadfiles/"+mf.getMphotosfn());
			File file = new File(deleteFileName);
			file.delete();
			
			String fileName = sc.getRealPath("/uploadfiles/"+originalFileName);
			
			InputStream is = mf.getMphoto().getInputStream();
			String sfn = FileSaver.save(fileName, is);	
			String savedFileName = sfn.substring(sfn.lastIndexOf("\\")+1);
			
			member.setMphotoofn(originalFileName);
			member.setMphotosfn(savedFileName);
			memberDAO.updatePhoto(member);
			
		} else {
			member.setMphotoofn(user.getUserInfo().getMphotoofn());
			member.setMphotosfn(user.getUserInfo().getMphotosfn());
		}
		
		mf.setMid(member.getMid());
		mf.setMpass(member.getMpass());
		mf.setMname(member.getMname());
		mf.setLvno(""+member.getLvno());
		mf.setMregdate(member.getMregdate());

		mf.setMbirth(member.getMbirth());
		mf.setMemail(member.getMemail());
		mf.setMaddressh(member.getMaddressh());
		mf.setMaddresso(member.getMaddresso());
		mf.setMphoneh(member.getMphoneh());
		mf.setMphoneo(member.getMphoneo());
		mf.setMintro(member.getMintro());
		
		mf.setMphotoofn(member.getMphotoofn());
		mf.setMphotosfn(member.getMphotosfn());
			
		mf.setMobirth(member.getMobirth());
		mf.setMoemail(member.getMoemail());
		mf.setMoaddressh(member.getMoaddressh());
		mf.setMoaddresso(member.getMoaddresso());
		mf.setMophoneh(member.getMophoneh());
		mf.setMophoneo(member.getMophoneo());
		mf.setMophoto(member.getMophoto());
		mf.setMointro(member.getMointro());
		mf.setMomailing(member.getMomailing());
		
		
		user.setUserInfo(memberDAO.getMemberByMid(user.getMid()));
		
		return mapping.findForward("updated");
	}

	public ActionForward view(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
//	
	MemberForm mf = (MemberForm)form;
	MemberDAO memberDAO = new MemberDAO();
	User user = (User)request.getSession().getAttribute("user");
	
	String mid = request.getParameter("mid");
	System.out.println(mid);
	Member member = memberDAO.getMemberByMid(mid);
	
//	setting data into form
	mf.setMid(member.getMid());
	mf.setMpass(member.getMpass());
	mf.setMname(member.getMname());
	mf.setLvno(""+member.getLvno());
	mf.setMregdate(member.getMregdate());

	mf.setMbirth(member.getMbirth());
	mf.setMemail(member.getMemail());
	mf.setMaddressh(member.getMaddressh());
	mf.setMaddresso(member.getMaddresso());
	mf.setMphoneh(member.getMphoneh());
	mf.setMphoneo(member.getMphoneo());
	mf.setMintro(member.getMintro());
	mf.setMphotoofn(member.getMphotoofn());
	mf.setMphotosfn(member.getMphotosfn());
					
	mf.setMobirth(member.getMobirth());
	mf.setMoemail(member.getMoemail());
	mf.setMoaddressh(member.getMoaddressh());
	mf.setMoaddresso(member.getMoaddresso());
	mf.setMophoneh(member.getMophoneh());
	mf.setMophoneo(member.getMophoneo());
	mf.setMophoto(member.getMophoto());
	mf.setMointro(member.getMointro());
	mf.setMomailing(member.getMomailing());
	
	
//	
	user.setUserInfo(memberDAO.getMemberByMid(user.getMid()));
	return mapping.findForward("updated");
}
	
	public ActionForward viewInfo(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String mid = request.getParameter("mid");
		MemberDAO memberDAO = new MemberDAO();
		Member member = memberDAO.getMemberByMid(mid);
		
		MemberForm mf = (MemberForm)form;
		
		User info = new User();
		info.setUserInfo(member);
//		setting data into form
		mf.setMid(member.getMid());
		mf.setMname(member.getMname());
		mf.setLvno(""+member.getLvno());
		mf.setMregdate(member.getMregdate());

		if(member.getMobirth().equals("on")) mf.setMbirth(member.getMbirth());
		if(member.getMoemail().equals("on")) mf.setMemail(member.getMemail());
		if(member.getMoaddressh().equals("on")) mf.setMaddressh(member.getMaddressh());
		if(member.getMoaddresso().equals("on")) mf.setMaddresso(member.getMaddresso());
		if(member.getMophoneh().equals("on")) mf.setMphoneh(member.getMphoneh());
		if(member.getMophoneo().equals("on")) mf.setMphoneo(member.getMphoneo());
		if(member.getMointro().equals("on")) mf.setMintro(member.getMintro());
		if(member.getMophoto().equals("on")) {
			mf.setMphotoofn(member.getMphotoofn());
			mf.setMphotosfn(member.getMphotosfn());
		}
		
		return mapping.findForward("memberinfo");
	}
	
}
